<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>用VS开发PHP扩展 - 大圣的笑 - 博客园</title>
<link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=VDh8zSH1vx51MDqRT7hK220akQ58FjlaaeGuWBPhfOA1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/iMetro_HD/bundle-iMetro_HD.css?v=9G-AuFYmpxqkeOPQcw_WAuPfB2xUfxYK7gabXn6lD881"/>
<link type="text/css" rel="stylesheet" href="/blog/customcss/140047.css?v=KyNzYlj9iKZT6gVO2Y0skak8zf0%3d"/>
<link title="RSS" type="application/rss+xml" rel="alternate" href="http://www.cnblogs.com/mahatmasmile/rss"/>
<link title="RSD" type="application/rsd+xml" rel="EditURI" href="http://www.cnblogs.com/mahatmasmile/rsd.xml"/>
<link type="application/wlwmanifest+xml" rel="wlwmanifest" href="http://www.cnblogs.com/mahatmasmile/wlwmanifest.xml"/>
<script src="http://common.cnblogs.com/script/jquery.js" type="text/javascript"></script>  
<script type="text/javascript">var currentBlogApp = 'mahatmasmile', cb_enable_mathjax=false;</script>
<script src="/bundles/blog-common.js?v=lWYisON4NCZPE0cDhRSZfCJYBtCYvhrzIJwgrI08A5U1" type="text/javascript"></script>
</head>
<body>
<a name="top"></a>

<!--done-->
<div id="home">
<div id="header">
	<div id="blogTitle">
	<a id="lnkBlogLogo" href="http://www.cnblogs.com/mahatmasmile/"><img id="blogLogo" src="/Skins/custom/images/logo.gif" alt="返回主页" /></a>			
		
<!--done-->
<h1><a id="Header1_HeaderTitle" class="headermaintitle" href="http://www.cnblogs.com/mahatmasmile/">大圣的笑</a></h1>
<h2>Σ(coding(i)*success(i))½</h2>



		
	</div><!--end: blogTitle 博客的标题和副标题 -->
	<div id="navigator">
		
<ul id="navList">
<li><a id="MyLinks1_HomeLink" class="menu" href="http://www.cnblogs.com/">博客园</a></li>
<li><a id="MyLinks1_MyHomeLink" class="menu" href="http://www.cnblogs.com/mahatmasmile/">首页</a></li>
<li><a id="MyLinks1_NewPostLink" class="menu" rel="nofollow" href="http://i.cnblogs.com/EditPosts.aspx?opt=1">新随笔</a></li>
<li><a id="MyLinks1_ContactLink" class="menu" rel="nofollow" href="http://msg.cnblogs.com/send/%E5%A4%A7%E5%9C%A3%E7%9A%84%E7%AC%91">联系</a></li>
<li><a id="MyLinks1_Syndication" class="menu" href="http://www.cnblogs.com/mahatmasmile/rss">订阅</a>
<!--<a id="MyLinks1_XMLLink" class="aHeaderXML" href="http://www.cnblogs.com/mahatmasmile/rss"><img src="http://www.cnblogs.com/images/xml.gif" alt="订阅" /></a>--></li>
<li><a id="MyLinks1_Admin" class="menu" rel="nofollow" href="http://i.cnblogs.com/">管理</a></li>
</ul>
		<div class="blogStats">
			
			
<!--done-->
随笔- 53&nbsp;
文章- 0&nbsp;
评论- 9&nbsp;

			
		</div><!--end: blogStats -->
	</div><!--end: navigator 博客导航栏 -->
</div><!--end: header 头部 -->

<div id="main">
	<div id="mainContent">
	<div class="forFlow">
		
<div id="post_detail">
<!--done-->
<div id="topics">
	<div class = "post">
		<h1 class = "postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="http://www.cnblogs.com/mahatmasmile/p/4527495.html">用VS开发PHP扩展</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body"><p><strong><span style="color: #ff0000;">开发前准备工作：</span></strong></p>
<p>VS（我用的2013）</p>
<p>Cygwin（下载地址：http://www.cygwin.com/）</p>
<p>搭载了php运行环境的IIS7.5 （用来测试）</p>
<p>php编译后的程序和编译前的源码，我用的当前最新版本5.6.9（下载地址：http://windows.php.net/download/）</p>
<p>编译后的程序路径：E:\php-5.6.9-nts-Win32-VC11-x86\</p>
<p>编译前的源码路径：E:\php-5.6.9-src\</p>
<p><strong><span style="color: #ff0000;">步骤：</span></strong></p>
<p><span style="color: #000000;">1.安装Cygwin</span></p>
<p><span style="color: #000000;"><img src="http://images0.cnblogs.com/blog2015/489995/201505/251135199596329.png" alt="" /></span></p>
<p>&nbsp;</p>
<p>从网络安装</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251135321463451.png" alt="" /></p>
<p>默认安装C盘</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251135483651506.png" alt="" /></p>
<p>下载缓存随意，安装结束记得删掉，我是放在桌面的</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251136023348374.png" alt="" /></p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251136080687392.png" alt="" /></p>
<p>在中国，就选http://mirrors.63.com，后面一直下一步到结束。</p>
<p>2.找到php源码目录我的是（E:\php-5.6.9-src，以下用这个代表源码目录），打开E:\php-5.6.9-src\ext\ext_skel_win32.php</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251422492565045.png" alt="" /></p>
<p>这里改成你的cygwin安装目录，我的就是C盘，所以不用改。</p>
<p>3.运行cmd，进入E:\php-5.6.9-src\ext\里，运行php.exe&nbsp;&nbsp;ext_skel_win32.php --extname=test，<span style="color: #ff0000;">这里test代表你的php扩展名</span>。</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251425490994522.png" alt="" /></p>
<p>打开E:\php-5.6.9-src\ext\就会看到一个test文件夹，这个就是你的扩展。</p>
<p>&nbsp;4.打开VS选择&ldquo;文件&rdquo;--&ldquo;新建&rdquo;--&ldquo;从现有代码创建目录&rdquo;</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251434364909841.png" alt="" /></p>
<p>选择C++</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251435074742409.png" alt="" /></p>
<p>这里选择你的php扩展文件夹路径，并且给项目命名</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251435143652113.png" alt="" /></p>
<p>选择&ldquo;使用 visual studio&rdquo;，项目类型选择&ldquo;动态链接库（DLL）项目&rdquo;，后面一直默认下一步一直到完成。</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251435215535076.png" alt="" /></p>
<p>5.刚开始打开会有很多错误，我们接下来开始配置项目。</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251438248651912.png" alt="" /></p>
<p>先把项目解决方案配置改为Release</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251440117874077.png" alt="" /></p>
<p>右键项目属性，C/C++，常规，附加包含目录，编辑</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251444203659118.png" alt="" /></p>
<p>加入以下几个php源码目录（实际目录以开发者自己的目录为准）：</p>
<p>E:\php-5.6.9-src</p>
<p>E:\php-5.6.9-src\main</p>
<p>E:\php-5.6.9-src\TSRM</p>
<p>E:\php-5.6.9-src\Zend</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251447183961009.png" alt="" /></p>
<p>右键项目属性，C/C++，预处理器，预处理器定义，编辑，加入以下变量：</p>
<p>ZEND_DEBUG=0</p>
<p>PHP_EXTENSION</p>
<p>PHP_WIN32</p>
<p>ZEND_WIN32</p>
<p>HAVE_<strong><span style="color: #ff0000;">TEST</span></strong>=1（<span style="color: #ff0000;">这里红色部分，要改成你的扩展名称，不改成你的扩展名，php会不识别</span>）</p>
<p>COMPILE_DL_<strong><span style="color: #ff0000;">TEST</span></strong>（<span style="color: #ff0000;">这里红色部分，要改成你的扩展名称，不改成你的扩展名，php会不识别</span>）</p>
<p>ZTS（<span style="color: #ff0000;">这一个变量加上是开启线程安全，不加是关闭线程安全</span>）</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251449485684120.png" alt="" /></p>
<p>生成解决方案，错误信息显示找不到&ldquo;config.w32.h&rdquo;，在源代码文件目录里搜索&ldquo;config.w32.h&rdquo;，在E:\php-5.6.9-src\win32\build\文件夹里找到&ldquo;config.w32.h.in&rdquo;，将这个文件复制到E:\php-5.6.9-src\main\文件夹里，去掉后面的&ldquo;.in&rdquo;</p>
<p>再次生成解决方案，显示错误信息<span style="color: #ff0000;">LNK1120</span></p>
<p><span style="color: #ff0000;">错误 7 error LNK1120: 5 个无法解析的外部命令 E:\php-5.6.9-src\ext\test\Release\phptest.dll 1 1 phptest</span></p>
<p>右键项目属性，连接器，输入，附加依赖项，编辑，将php5.lib的路径放进去（这个文件在php编译后的程序文件夹里，根目录的dev文件夹里）</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251504371624124.png" alt="" /></p>
<p>注意：为了让扩展能和php运行环境匹配，要根据你的php运行环境的编译版本（php5.6.9是VC11编译的），在E:\php-5.6.9-src\main\文件夹里的config.w32.h文件里加上：</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">#define</span> PHP_COMPILER_ID "VC11" </pre>
</div>
<p>&nbsp;</p>
<p><span style="line-height: 1.5;">打开test.c</span></p>
<p>找到这一段代码：</p>
<div class="cnblogs_code">
<pre><span style="color: #000000;">PHP_FUNCTION(confirm_test_compiled)
{
    </span><span style="color: #0000ff;">char</span> *arg =<span style="color: #000000;"> NULL;
    </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> arg_len, len;
    </span><span style="color: #0000ff;">char</span> *<span style="color: #000000;">strg;

    </span><span style="color: #0000ff;">if</span> (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, <span style="color: #800000;">"</span><span style="color: #800000;">s</span><span style="color: #800000;">"</span>, &amp;arg, &amp;arg_len) ==<span style="color: #000000;"> FAILURE) {
        </span><span style="color: #0000ff;">return</span><span style="color: #000000;">;
    }

    len </span>= spprintf(&amp;strg, <span style="color: #800080;">0</span>, <span style="color: #800000;">"</span><span style="color: #800000;">Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">test</span><span style="color: #800000;">"</span><span style="color: #000000;">, arg);
    RETURN_STRINGL(strg, len, </span><span style="color: #800080;">0</span><span style="color: #000000;">);
}</span></pre>
</div>
<p>将<span style="color: #ff0000;">confirm_test_compiled<span style="color: #000000;">改成<span style="color: #ff0000;">test_echo</span></span></span></p>
<p>再找到这一段代码：</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">const</span> zend_function_entry test_functions[] =<span style="color: #000000;"> {
    PHP_FE(confirm_test_compiled,    NULL)        </span><span style="color: #008000;">/*</span><span style="color: #008000;"> For testing, remove later. </span><span style="color: #008000;">*/</span><span style="color: #000000;">
    PHP_FE_END    </span><span style="color: #008000;">/*</span><span style="color: #008000;"> Must be the last line in test_functions[] </span><span style="color: #008000;">*/</span><span style="color: #000000;">
};</span></pre>
</div>
<p>将里面的<span style="color: #ff0000;">confirm_test_compiled</span>也改成<span style="color: #ff0000;">test_echo</span></p>
<p>生成解决方案，在项目根目录的Release文件夹里找到自己的php扩展phptest.dll，复制到php的ext文件夹里，在php.ini里配置上：</p>
<p>extension=phptest.dll</p>
<p>重启IIS，新建一个站点，在里面新建一个test.php文件</p>
<div class="cnblogs_code">
<pre>&lt;?<span style="color: #000000;">php 
</span><span style="color: #0000ff;">echo</span> test_echo("123");</pre>
</div>
<p>运行得到结果：</p>
<p><img src="http://images0.cnblogs.com/blog2015/489995/201505/251532080218337.png" alt="" /></p>
<p>这个test_echo函数，就是我们自己的自定义函数了，你也可以根据需求，开发自己的扩展来提高php的性能。</p>
<p>&nbsp;</p></div><div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory"></div>
<div id="EntryTag"></div>
<div id="blog_post_info">
</div>
<div class="clear"></div>
<div id="post_next_prev"></div>
</div>


		</div>
		<div class = "postDesc">posted @ <span id="post-date">2015-05-25 15:33</span> <a href='http://www.cnblogs.com/mahatmasmile/'>大圣的笑</a> 阅读(<span id="post_view_count">...</span>) 评论(<span id="post_comment_count">...</span>)  <a href ="http://i.cnblogs.com/EditPosts.aspx?postid=4527495" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(4527495);return false;">收藏</a></div>
	</div>
	<script type="text/javascript">var allowComments=true,isLogined=true,cb_blogId=140047,cb_entryId=4527495,cb_blogApp=currentBlogApp,cb_blogUserGuid='3669af81-b05f-e211-aa8f-842b2b196315',cb_entryCreatedDate='2015/5/25 15:33:00';loadViewCount(cb_entryId);</script>
	
</div><!--end: topics 文章、评论容器-->
</div><a name="!comments"></a><div id="blog-comments-placeholder"></div><script type="text/javascript">var commentManager = new blogCommentManager();commentManager.renderComments(0);</script>
<div id="comment_form" class="commentform">
<a name="commentform"></a>
<div id="divCommentShow"></div>
<div id="comment_nav"><span id="span_refresh_tips"></span><a href="javascript:void(0);" id="lnk_RefreshComments" onclick="return RefreshCommentList();">刷新评论</a><a href="#" onclick="return RefreshPage();">刷新页面</a><a href="#top">返回顶部</a></div>
<div id="comment_form_container"></div>
<div class="ad_text_commentbox" id="ad_text_under_commentbox"></div>
<div id="site_nav_under"><a href="http://www.cnblogs.com/" target="_blank" title="开发者的网上家园">博客园首页</a><a href="http://q.cnblogs.com/" target="_blank" title="程序员问答社区">博问</a><a href="http://news.cnblogs.com/" target="_blank" title="IT新闻">新闻</a><a href="http://home.cnblogs.com/ing/" target="_blank">闪存</a><a href="http://job.cnblogs.com/" target="_blank">程序员招聘</a><a href="http://kb.cnblogs.com/" target="_blank">知识库</a></div>
<div id="opt_under_post"></div>
<script type="text/javascript">
    var enableGoogleAd = canShowAdsense(); 
    fixPostBodyFormat();
</script>
<script type='text/javascript'>
  var googletag = googletag || {};
  googletag.cmd = googletag.cmd || [];
    (function () {
        if (enableGoogleAd) {
            var gads = document.createElement('script');
            gads.async = true;
            gads.type = 'text/javascript';
            var useSSL = 'https:' == document.location.protocol;
            gads.src = (useSSL ? 'https:' : 'http:') +
              '//www.googletagservices.com/tag/js/gpt.js';
            var node = document.getElementsByTagName('script')[0];
            node.parentNode.insertBefore(gads, node);
        }
  })();
</script>
<script type='text/javascript'>
    try {
        if (enableGoogleAd) {
            googletag.cmd.push(function() {
            googletag.defineSlot('/1090369/cnblogs_blogpost_C2', [468, 60], 'div-gpt-ad-1433581717989-0').addService(googletag.pubads());
            googletag.defineSlot('/1090369/cnblogs_blogpost_C1_sitehome', [300, 250], 'div-gpt-ad-1433581717989-1').addService(googletag.pubads());
            googletag.pubads().enableSingleRequest();
            googletag.pubads().collapseEmptyDivs();
            googletag.enableServices();
            });
        };
    } catch (e) { }
</script>
<div id="google_ad_c1" class="c_ad_block">
    <div id='div-gpt-ad-1433581717989-1' style='height:250px; width:300px;'>
    <script type='text/javascript'>
        try {
            if (enableGoogleAd) {
                googletag.cmd.push(function () { googletag.display('div-gpt-ad-1433581717989-1'); });
            } else {
                $('#div-gpt-ad-1433581717989-1').hide();
            }
    } catch (e) { }
    </script>
    </div>
</div>
<div id="under_post_news"></div>
<div id="google_ad_c2" class="c_ad_block">
<div id='div-gpt-ad-1433581717989-0' style='height:60px; width:468px;'>
<script type='text/javascript'>
try {
    if (enableGoogleAd) {
        googletag.cmd.push(function () { googletag.display('div-gpt-ad-1433581717989-0'); });
    } else {
        $('#div-gpt-ad-1433581717989-0').hide();
    }
} catch (e) { }
</script>
</div>
</div>
<div id="under_post_kb"></div>
<div id="HistoryToday" class="c_ad_block"></div>
<script type="text/javascript">
$(function () {
    loadNewsAndKb();
    loadBlogSignature();
    LoadPostInfoBlock(cb_blogId, cb_entryId, cb_blogApp, cb_blogUserGuid);
    GetPrevNextPost(cb_entryId, cb_blogId, cb_entryCreatedDate);
    loadOptUnderPost();
    GetHistoryToday(cb_blogId, cb_blogApp, cb_entryCreatedDate);
    setTimeout(function () { incrementViewCount(cb_entryId); }, 200);
});
</script>
</div>

	</div><!--end: forFlow -->
	</div><!--end: mainContent 主体内容容器-->

	<div id="sideBar">
		<div id="sideBarMain">
			
<!--done-->
<div class="newsItem">
<h3 class="catListTitle">公告</h3>
	<div id="blog-news"></div><script type="text/javascript">loadBlogNews();</script>
</div>

			<div id="blog-calendar" style="display:none"></div><script type="text/javascript">loadBlogDefaultCalendar();</script>
			
			<div id="leftcontentcontainer">
				<div id="blog-sidecolumn"></div><script type="text/javascript">loadBlogSideColumn();</script>
			</div>
			
		</div><!--end: sideBarMain -->
	</div><!--end: sideBar 侧边栏容器 -->
	<div class="clear"></div>
	</div><!--end: main -->
	<div class="clear"></div>
	<div id="footer">
		
<!--done-->
Copyright &copy;2015 大圣的笑
	</div><!--end: footer -->
</div><!--end: home 自定义的最大容器 -->
</body>
</html>
